$(document).ready(function() {
    $modal = $("#modal");
   
    $('.popup').on('click', function() {
        var $modal = $("#" + $(this).attr("target"));
        var url = $(this).attr("url");
        if ($(this).attr("xid") != "undefined") {
            var xid = $(this).attr("xid");
            var url = url + "?id=" + xid;
        }
        $('body').modalmanager('loading');

        $modal.load(url, '', function() {
            $modal.modal();
            $('body').modalmanager('loading');
        });

    });

    $('.salvar').on('click', function() {

        $modal.modalmanager('loading');
        var formid = $(this).attr("xform");
        var url = $(this).attr("url");
        if ($(this).attr("xid") != "undefined") {
            var xid = $(this).attr("xid");
            var url = url + "?id=" + xid;
        }
        var $inputs = $('#' + formid + ' :input');
        var ivals = {};
        $inputs.each(function() {
            ivals[this.name] = $(this).val();
        });

        $.post("salvar.do", ivals);


        $modal.load(url, '', function() {
            $modal.modal();
            $("#info").html("<div class='alert alert-success'><button type='button' class='close' data-dismiss='alert'>×</button><strong>Atualizações salvas!</strong></div>");

        });

    });
    $('.del').on('click', function() {
        var data = "type=" + $(this).attr("type") + "&id=" + $(this).attr("xid") + "&status=" + $(this).attr("status");        
        $.ajax({
            type: "GET",
            url: "remover.do",
            data: data,
            success: function(e) {
                location.reload();
            }
        });
    });
    $('.fechar').on('click', function() {
        if ($("#info").html() != "")
            location.reload();
    });
});
    